package com.qiyu.bigdata.utils;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.ArrayList;
import java.util.List;

public class ExcelUtil {
    public static List<String> getSheets(Workbook wb){
        int numberOfSheets = wb.getNumberOfSheets();
        ArrayList<String> sheetNames = new ArrayList<>();
        for (int i = 0 ; i < numberOfSheets; i++){
            sheetNames.add(wb.getSheetName(i));
        }
        return sheetNames;
    }

    public static String getValue(Cell cell){
        CellType type = cell.getCellType();
        String value = null;
        switch(type){
            case STRING: value = cell.getStringCellValue();break;
            case NUMERIC: value = "" + cell.getNumericCellValue();break;
            case FORMULA: value = cell.getCellFormula();break;
            default: value = "";
        }
        return value;
    }

    public static void setValue(Cell cell, String value){
            try {
                cell.setCellValue(Double.parseDouble(value));
            } catch (NumberFormatException | NullPointerException ex) {
                cell.setCellValue(value);
            }
    }
    
    
    public static boolean isMergedCell(List<CellRangeAddress> mergedRegions , Cell cell){
        for (CellRangeAddress mergedRegion : mergedRegions) {
            int rowIndex = cell.getRowIndex();
            int columnIndex = cell.getColumnIndex();
            if(rowIndex>= mergedRegion.getFirstRow() && columnIndex > mergedRegion.getFirstColumn() && rowIndex <= mergedRegion.getLastRow() && columnIndex<= mergedRegion.getLastColumn()){
                return true;
            }
        }
        return false;
    }

}
